﻿namespace Deferred.Library.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    public delegate void OptionCallback(bool value);
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="value"></param>
    public delegate void OptionChangedEvent(Option sender, bool value);

    /// <summary>
    /// 
    /// </summary>
    public class Option
    {
        private bool value;
        /// <summary>
        /// 
        /// </summary>
        public string Category { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public bool Value
        {
            get { return value; }
            set
            {
                this.value = value;
                ValueChanged(this, value);
                if (Callback != null) Callback(value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public OptionCallback Callback { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public event OptionChangedEvent ValueChanged = delegate { };
        /// <summary>
        /// 
        /// </summary>
        public string FullName { get { return Category + "." + Name; } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FullName"></param>
        /// <param name="Value"></param>
        public Option(string FullName, bool Value)
            : this(FullName, Value, null) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FullName"></param>
        /// <param name="Value"></param>
        /// <param name="Callback"></param>
        public Option(string FullName, bool Value, OptionCallback Callback)
        {
            int index = FullName.IndexOf('.');
            this.Category = FullName.Substring(0, index);
            this.Name = FullName.Substring(index + 1);
            this.value = Value;
            this.Callback = Callback;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static string GetFullName(string Category, string Name) { return Category + "." + Name; }
    }
}
